import {sys} from "cc";
import {GlobalConst} from "db://assets/scripts/consts/GlobalConst";

const wx = window['wx'];

export function get<T>(url: string, param?: {}): Promise<T> {
    console.log("发起请求", GlobalConst.api + url, param)

    if (wx == null) {
        if (param != null) {
            const queryParam = new URLSearchParams(param).toString();
            url = url + "?" + queryParam;
        }
        return new Promise<T>((resolve, reject) => {
             fetch(GlobalConst.api + url, {
                headers: {
                    "token": sys.localStorage.getItem('token')
                }
            })
                .then((response: Response) => {
                    return response.json()
                }).then((res) => {
                    console.log(url + "响应", res)
                    resolve(res.data)
                }).catch((error) => {
                    console.log(url + "错误", error)
                    reject(error);
                })
        })
    }
    return new Promise((resolve, reject) => {
        wx.request({
            url: GlobalConst.api + url,
            method: "GET",
            header: {
                "token": sys.localStorage.getItem('token')
            },
            data: param,
            success: (res) => {
                if (res.statusCode) {
                    console.log(url + "响应", res.data.data)
                    resolve(res.data.data)
                } else {
                    console.log("请求错误", res.errMsg);
                    reject(res);
                }
            },
            fail: (err) => {
                console.error('请求错误', err);
                reject(err);

            }
        })
    })
}

export function post<T>(url: string, data?: {}): Promise<T> {
    console.log("发起请求", GlobalConst.api + url, data)
    if (wx == null) {
        return new Promise<T>((resolve, reject) => {
             fetch(GlobalConst.api + url, {
                method: "POST",
                headers: {
                    'Content-Type': 'application/json',
                    "token": sys.localStorage.getItem('token')
                },
                body: JSON.stringify(data)
            })
                .then((response: Response) => {
                    return response.json()
                }).then((res) => {
                    console.log(url + "响应", res)
                    resolve(res.data)
                }).catch((error) => {
                    console.log(url + "错误", error)
                    reject(error);
                })
        })
    }

    return new Promise((resolve, reject) => {
        wx.request({
            url: GlobalConst.api + url,
            method: "POST",
            header: {
                "token": sys.localStorage.getItem('token')
            },
            data,
            success: (res) => {
                if (res.statusCode) {
                    resolve(res.data.data)
                } else {
                    console.log("请求错误", res.errMsg);
                    reject(res);
                }
            },
            fail: (err) => {
                console.error('请求错误', err);
                reject(err);

            }
        })
    })
}